package com.xiaohei.crm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohei.crm.common.exception.XiaoheiException;
import com.xiaohei.crm.common.result.Result;
import com.xiaohei.crm.dao.Inventory;
import com.xiaohei.crm.mapper.InventoryMapper;
import com.xiaohei.crm.service.InventoryService;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.xiaohei.crm.common.RedisEnums.INVENTORY_REGISTRY_KEY;
import static com.xiaohei.crm.common.RedisEnums.INVENTORY_UPDATE_LOCK;
import static com.xiaohei.crm.common.result.ResultCodeEnum.DATA_ERROR;

/**
 * ClassName: InventoryServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/6/23 18:02
 * Version 1.0
 */
@Service
@RequiredArgsConstructor
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    private final InventoryMapper inventoryMapper;
    private final StringRedisTemplate stringRedisTemplate;
    private final RedissonClient redissonClient;

    @Override
    public Result<IPage> findByPage() {
        Page<Inventory> page = new Page<>();
        LambdaQueryWrapper<Inventory> eq = Wrappers.lambdaQuery(Inventory.class).eq(Inventory::getDelFlag, 0);
        return Result.ok(inventoryMapper.selectPage(page, eq));
    }

    @Override
    public void saveInventory(Inventory inventory) {
        String s = stringRedisTemplate.opsForValue()
                .get(INVENTORY_REGISTRY_KEY + inventory.getInventoryId());
        if (!StrUtil.isEmpty(s)) {
            throw new XiaoheiException(DATA_ERROR);
        }

        int isInsert = baseMapper.insert(inventory);
        if (isInsert < 1) {
            throw new XiaoheiException(DATA_ERROR);
        }

        stringRedisTemplate.opsForValue()
                .set(INVENTORY_REGISTRY_KEY + inventory.getInventoryId(),inventory.getInventoryId().toString());
    }

    @Override
    public void updateInventoryById(Inventory inventory) {
        RLock lock = redissonClient.getLock(INVENTORY_UPDATE_LOCK);
        lock.lock();
        try {
            String s = stringRedisTemplate.opsForValue()
                    .get(INVENTORY_REGISTRY_KEY + inventory.getInventoryId());
            if (StrUtil.isEmpty(s)) {
                throw new XiaoheiException(DATA_ERROR);
            }

            LambdaUpdateWrapper<Inventory> eq =
                    Wrappers.lambdaUpdate(Inventory.class)
                            .eq(Inventory::getInventoryId, inventory.getInventoryId());
            baseMapper.update(inventory, eq);
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }

    @Override
    public void removeByInventoryId(String id) {
        String s = stringRedisTemplate.opsForValue()
                .get(INVENTORY_REGISTRY_KEY + id);
        if (StrUtil.isEmpty(s)) {
            throw new XiaoheiException(DATA_ERROR);
        }

        //只有数量为零才可以删除
        LambdaUpdateWrapper<Inventory> updateWrapper =
                Wrappers.lambdaUpdate(Inventory.class)
                        .eq(Inventory::getMaterialNumber,0)
                        .eq(Inventory::getInventoryId, id);
        Inventory inventory = Inventory.builder().delFlag(1).build();
        int update = baseMapper.update(inventory, updateWrapper);

        if(update < 1){
            throw new XiaoheiException(DATA_ERROR);
        }

        stringRedisTemplate.delete(INVENTORY_REGISTRY_KEY + id);
    }

}
